#import "MouseCheeseGame.h"

@interface MouseCheeseGame ()

@property BOOL isGameOver;

@end

@implementation MouseCheeseGame

-(instancetype) init {
    self = [super init];
    if(self) {
        self.name = @"Mouse Cheese Game";
        self.type = observation;
        self.ruleOfTheGame = @"Can the mouse get the cheese?";
        self.isGameOver = NO;
    }
    return self;
}

-(void) draw {
    NSInteger imageNum = arc4random_uniform(12);
    NSString* startImage = [NSString stringWithFormat:@"mouse%ld",imageNum];
    ImageGameComponent* image = [[ImageGameComponent alloc] initWithName:startImage andFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 2*kButtonHeigh) andTrans:0];
    [self addComponents:image];
}

-(void)changeAnImage{
    int imageNumber=arc4random_uniform(12);
    NSString* imageString=[NSString stringWithFormat:@"mouse%d",imageNumber+1];
    ((ImageGameComponent*)(self.components[0])).name = imageString;
}

-(void)changeImage:(NSTimer*)timer{
    if (self.isGameOver==YES) {
        [timer invalidate];
    }
    else{
        [self changeAnImage];
        
        [[NSNotificationCenter defaultCenter]postNotificationName:@"SomeGames" object:self userInfo:nil];
        
    }
}

-(void) play {
    [self draw];
    
    [NSTimer scheduledTimerWithTimeInterval: 4 target: self selector:@selector(changeImage:)userInfo: nil repeats:YES];
}

-(BOOL) isRight {
    NSString* currentImage = ((ImageGameComponent*) self.components[0]).name;
    return [currentImage isEqual:@"mouse6"] || [currentImage isEqual:@"mouse7"] || [currentImage isEqual:@"mouse1"] || [currentImage isEqual:@"mouse8"] || [currentImage isEqual:@"mouse13"];
}

-(void) gameOver {
    self.isGameOver = YES;
    
}

@end
